package com.xian.im.modules.command.group;

import com.xian.feign.GroupChannelClient;
import com.xian.feign.UserChannelClient;
import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.exception.BusinessException;
import com.xian.im.common.model.dto.GroupDto;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.avchat.manager.ImGroupManager;

import java.security.NoSuchAlgorithmException;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-27 09:26
 **/
public abstract class AbsGroupCommand implements GroupCommand {
    /**
     * 是不是管理员
     *
     * @return
     */
    protected Boolean isManager(MsgRequest request, String ctxId, GroupDto groupEntity) {
        SessionUser session = getSession(ctxId);
        return SpringContextHolder.getBean(ImGroupManager.class).isManager(session.getAccount(), groupEntity.getGroupUniqueId());
    }

    /**
     * 是不是群主
     *
     * @param
     * @param groupEntity
     * @return
     */
    protected Boolean isOwnerAccount(MsgRequest request, String ctxId, GroupDto groupEntity) {
        SessionUser session = getSession(ctxId);
        return session.getAccount().equals(groupEntity.getOwnerAccount());
    }

    /**
     * 群组业务
     *
     * @param request
     * @param ctxId
     * @param groupEntity
     */
    @Override
    public void exec(MsgRequest request, String ctxId, GroupDto groupEntity) throws NoSuchAlgorithmException {

        String cmdCode = request.getCmdCode();
        UserChannelClient client = SpringContextHolder.getBean(UserChannelClient.class);
        if (!client.hasGroup(request.getGroupUniqueId(), ctxId)) {
            if(CommandCodeConstant.GROUP_JOIN.equals(cmdCode)){
                handler(request, ctxId, groupEntity);
                return;
            }else {
                throw new BusinessException("请先加入群组，操作失败");
            }
        }
        // 踢出多端连接 群组消息禁言 群成员禁言 群成员接触禁言   群解除禁言
        if (CommandCodeConstant.GROUP_KICK.equals(cmdCode) ||
                CommandCodeConstant.GROUP_MUTE.equals(cmdCode) ||
                CommandCodeConstant.GROUP_MEMBER_MUTE.equals(cmdCode) ||
                CommandCodeConstant.GROUP_OPEN_MEMBER_MUTE.equals(cmdCode) ||
                CommandCodeConstant.GROUP_OPEN_MUTE.equals(cmdCode)) {
            if (isManager(request, ctxId, groupEntity) || isOwnerAccount(request, ctxId, groupEntity)) {
                handler(request, ctxId, groupEntity);
                return;
            } else {
                throw new BusinessException("权限不足");
            }
        }
        // 设置管理员  解除管理员
        if (CommandCodeConstant.GROUP_MANAGER.equals(cmdCode) || CommandCodeConstant.GROUP_REVOKE_MANAGER.equals(cmdCode)) {
            if (isOwnerAccount(request, ctxId, groupEntity)) {
                handler(request, ctxId, groupEntity);
                return;
            } else {
                throw new BusinessException("权限不足");
            }
        }
        handler(request, ctxId, groupEntity);
    }

    public abstract void handler(MsgRequest request, String ctxId, GroupDto groupEntity) throws NoSuchAlgorithmException;

    protected SessionUser getSession(String ctxId) {
        UserChannelClient client = SpringContextHolder.getBean(UserChannelClient.class);
        return client.session(ctxId);
    }

    protected UserChannelClient userChannelClient(){
        UserChannelClient client = SpringContextHolder.getBean(UserChannelClient.class);
        return client;
    }

    protected GroupChannelClient groupChannelClient(){
        GroupChannelClient channelClient = SpringContextHolder.getBean(GroupChannelClient.class);
        return channelClient;
    }
}
